/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.dao.DepartmentDao;
import com.minierp.dao.StafferDao;
import com.minierp.helper.EncCommonHelper;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class DepartmentMACtr extends MultiActionController {
	private DepartmentDao departmentDao;
	private StafferDao stafferDao;
	private String listView;
	private String listForSelectView;
	private String listForStafferSelectView;
	private String listForInnerPbView;

	public String getListForInnerPbView() {
		return this.listForInnerPbView;
	}

	public void setListForInnerPbView(String listForInnerPbView) {
		this.listForInnerPbView = listForInnerPbView;
	}

	public ModelAndView handleListForInnerPb(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForInnerPbView);
		List list = this.departmentDao.getList(true);
		mv.addObject("departmentList", list);
		String defaultId = request.getParameter("default_id");
		if (defaultId != null && !defaultId.equals("")) {
			mv.addObject("defaultId", defaultId);
		}

		return mv;
	}

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listView);
		List list = this.departmentDao.getList(true);
		mv.addObject("departmentList", list);
		String defaultId = request.getParameter("default_id");
		if (defaultId != null && !defaultId.equals("")) {
			mv.addObject("defaultId", defaultId);
		}

		return mv;
	}

	public ModelAndView handleListForSelect(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForSelectView);
		List list = this.departmentDao.getList(true);
		mv.addObject("departmentList", list);
		return mv;
	}

	public ModelAndView handleListForStafferSelect(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForStafferSelectView);
		List list = this.departmentDao.getList(true);
		mv.addObject("departmentList", list);
		String defaultId = request.getParameter("default_id");
		if (defaultId != null && !defaultId.equals("")) {
			mv.addObject("defaultId", defaultId);
		}

		String type = request.getParameter("type");
		mv.addObject("type", type);
		return mv;
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String returnUrl = request.getParameter("from_url");
		ModelAndView mv = new ModelAndView("redirect:" + returnUrl);
		if (!EncCommonHelper.checkPrivilege(request, "BASE_STAF")) {
			throw new Exception("Permission deny!");
		} else {
			String id = request.getParameter("id");
			int stafferNum = this.stafferDao.getCount(Long.parseLong(id));
			if (stafferNum > 0) {
				EncCommonHelper.setAlertMessage(request,
						"该部门或该部门的下属部门有员工存在，不能删除！");
			} else {
				this.departmentDao.unenabledTree(Long.parseLong(id));
				EncCommonHelper.setAlertMessage(request, "删除成功！");
			}

			return mv;
		}
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public String getListForStafferSelectView() {
		return this.listForStafferSelectView;
	}

	public void setListForStafferSelectView(String listForStafferSelectView) {
		this.listForStafferSelectView = listForStafferSelectView;
	}

	public StafferDao getStafferDao() {
		return this.stafferDao;
	}

	public void setStafferDao(StafferDao stafferDao) {
		this.stafferDao = stafferDao;
	}

	public String getListForSelectView() {
		return this.listForSelectView;
	}

	public void setListForSelectView(String listForSelectView) {
		this.listForSelectView = listForSelectView;
	}
}